import { Toast } from 'vant'

document.addEventListener('plusready', function () {
  var webview = plus.webview.currentWebview()
  plus.key.addEventListener('backbutton', function () {
    webview.canBack(function (e) {
      if (e.canBack) {
        webview.back()
      } else {
        // 首页返回键处理
        // 1.5秒内连续按两次返回键，则退出应用
        handleBackButton()
      }
    })
  })

  function handleBackButton() {
    var first = null
    plus.key.addEventListener(
      'backbutton',
      function () {
        if (!first) {
          first = new Date().getTime()
          Toast({
            message: '再按一次退出应用',
            position: 'bottom',
            duration: 1000
          })
          setTimeout(() => {
            first = null
          }, 1500)
        } else {
          if (new Date().getTime() - first < 1500) {
            plus.runtime.quit()
          }
        }
      },
      false
    )
  }
})
